package com.hyk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.SysRole;
import com.hyk.dto.R;
import com.hyk.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    //按条件分页查询角色列表
    @GetMapping
    @ApiOperation("按条件查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的大小"),
            @ApiImplicitParam(name = "name", value = "角色名称")
    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public R<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> page, String name){
        Page<SysRole> pageList =  sysRoleService.pageList(page,name);
        return R.ok(pageList);
    }

    //新建角色
    @PostMapping
    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称"),
            @ApiImplicitParam(name = "code", value = "角色代码")
    })
    @PreAuthorize("hasAuthority('sys_role_create')")
    public R add(@RequestBody @Validated SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    //删除角色
    @PostMapping("/delete")
    @ApiOperation("按条件查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "角色id数组")
    })
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public R delete(@RequestBody String[] ids){
        boolean removeByIds = sysRoleService.removeByIds(Arrays.asList(ids));
        if(removeByIds){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    //角色信息

}
